{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## enum"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### enum is used to create symbols for values instead of using strings and interger."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Member name: enroute_runway\n",
      "Member value: 1\n"
     ]
    }
   ],
   "source": [
    "# Creating enum class\n",
    "\n",
    "import enum\n",
    "\n",
    "class PlaneStatus(enum.Enum):\n",
    "    standing = 0\n",
    "    enroute_runway = 1\n",
    "    takeoff = 2\n",
    "    in_air = 3\n",
    "    landing = 4\n",
    "    \n",
    "\n",
    "print('\\nMember name: {}'.format(PlaneStatus.enroute_runway.name))\n",
    "print('Member value: {}'.format(PlaneStatus.enroute_runway.value))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "standing = 0\n",
      "enroute_runway = 1\n",
      "takeoff = 2\n",
      "in_air = 3\n",
      "landing = 4\n"
     ]
    }
   ],
   "source": [
    "#Iterating over enums\n",
    "\n",
    "for member in PlaneStatus:\n",
    "    print('{} = {}'.format(member.name, member.value))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Equality:  False True\n",
      "Identity:  False True\n"
     ]
    }
   ],
   "source": [
    "#Comparing enums - compare using identity and equality\n",
    "\n",
    "actual_state = PlaneStatus.enroute_runway\n",
    "desired_state = PlaneStatus.in_air\n",
    "\n",
    "#comparison through equality\n",
    "print('Equality: ', actual_state == desired_state, actual_state == PlaneStatus.enroute_runway)\n",
    "\n",
    "#comparison through identity\n",
    "print('Identity: ', actual_state is desired_state, actual_state is PlaneStatus.enroute_runway)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ordered by value:\n",
      "Cannot sort:'<' not supported between instances of 'PlaneStatus' and 'PlaneStatus'\n"
     ]
    }
   ],
   "source": [
    "'''NO SUPPORT FOR ORDERED SORTING AND COMPARISON'''\n",
    "\n",
    "print('Ordered by value:')\n",
    "try:\n",
    "    print('\\n'.join('' + s.name for s in sorted(PlaneStatus)))\n",
    "except TypeError as err:\n",
    "    print('Cannot sort:{}'.format(err))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Use IntEnum for order support"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " standing\n",
      " enroute_runway\n",
      " takeoff\n",
      " in_air\n",
      " landing\n"
     ]
    }
   ],
   "source": [
    "# Ordered by value\n",
    "class NewPlaneStatus(enum.IntEnum):\n",
    "    standing = 0\n",
    "    enroute_runway = 1\n",
    "    takeoff = 2\n",
    "    in_air = 3\n",
    "    landing = 4\n",
    "\n",
    "print('\\n'.join(' ' + s.name for s in sorted(NewPlaneStatus)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Unique Ebumeration values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "standing = 0\n",
      "enroute_runway = 1\n",
      "takeoff = 2\n",
      "in_air = 3\n",
      "landing = 4\n",
      "\n",
      "Same: standing is maintainance:  True\n",
      "Same: in_air is fueling:  True\n"
     ]
    }
   ],
   "source": [
    "#Aliases for other members, do not appear separately in the output when iterating over the Enum. \n",
    "#The canonical name for a member is the first name attached to the value.\n",
    "\n",
    "class SamePlaneStatus(enum.Enum):\n",
    "    standing = 0\n",
    "    enroute_runway = 1\n",
    "    takeoff = 2\n",
    "    in_air = 3\n",
    "    landing = 4\n",
    "    \n",
    "    maintainance = 0\n",
    "    fueling = 3\n",
    "    \n",
    "for status in SamePlaneStatus:\n",
    "    print('{} = {}'.format(status.name, status.value))\n",
    "    \n",
    "print('\\nSame: standing is maintainance: ', SamePlaneStatus.standing is SamePlaneStatus.maintainance)\n",
    "print('Same: in_air is fueling: ', SamePlaneStatus.in_air is SamePlaneStatus.fueling)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "duplicate values found in <enum 'UniPlaneStatus'>: maintainance -> standing, fueling -> in_air",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-7-b386d5056999>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;33m@\u001b[0m\u001b[0menum\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0munique\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[1;32mclass\u001b[0m \u001b[0mUniPlaneStatus\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0menum\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mEnum\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      5\u001b[0m     \u001b[0mstanding\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      6\u001b[0m     \u001b[0menroute_runway\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\enum.py\u001b[0m in \u001b[0;36munique\u001b[1;34m(enumeration)\u001b[0m\n\u001b[0;32m    832\u001b[0m                 [\"%s -> %s\" % (alias, name) for (alias, name) in duplicates])\n\u001b[0;32m    833\u001b[0m         raise ValueError('duplicate values found in %r: %s' %\n\u001b[1;32m--> 834\u001b[1;33m                 (enumeration, alias_details))\n\u001b[0m\u001b[0;32m    835\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0menumeration\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    836\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: duplicate values found in <enum 'UniPlaneStatus'>: maintainance -> standing, fueling -> in_air"
     ]
    }
   ],
   "source": [
    "# Add @unique decorator to the Enum\n",
    "\n",
    "@enum.unique\n",
    "class UniPlaneStatus(enum.Enum):\n",
    "    standing = 0\n",
    "    enroute_runway = 1\n",
    "    takeoff = 2\n",
    "    in_air = 3\n",
    "    landing = 4\n",
    "    \n",
    "    #error triggered here\n",
    "    maintainance = 0\n",
    "    fueling = 3\n",
    "    \n",
    "for status in SamePlaneStatus:\n",
    "    print('{} = {}'.format(status.name, status.value))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Creating Enums programmatically"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Member:PlaneStatus.in_air\n",
      "\n",
      "All Members:\n",
      "standing = 1\n",
      "enroute_runway = 2\n",
      "takeoff = 3\n",
      "in_air = 4\n",
      "landing = 5\n"
     ]
    }
   ],
   "source": [
    "PlaneStatus = enum.Enum(\n",
    "    value = 'PlaneStatus',\n",
    "    names = ('standing', 'enroute_runway', 'takeoff', 'in_air', 'landing')\n",
    ")\n",
    "\n",
    "print('Member:{}'.format(PlaneStatus.in_air))\n",
    "\n",
    "print('\\nAll Members:')\n",
    "for status in PlaneStatus:\n",
    "    print('{} = {}'.format(status.name, status.value))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "All Members:\n",
      "standing = 1\n",
      "enroute_runway = 2\n",
      "takeoff = 3\n",
      "in_air = 4\n",
      "landing = 5\n"
     ]
    }
   ],
   "source": [
    "PlaneStatus = enum.Enum(\n",
    "    value = 'PlaneStatus',\n",
    "    names = [\n",
    "        ('standing', 1),\n",
    "        ('enroute_runway', 2), \n",
    "        ('takeoff', 3), \n",
    "        ('in_air', 4), \n",
    "        ('landing', 5)\n",
    "    ]\n",
    ")\n",
    "\n",
    "print('\\nAll Members:')\n",
    "for status in PlaneStatus:\n",
    "    print('{} = {}'.format(status.name, status.value))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
